﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace belov_test
{
    public partial class UserPanel : UserControl
    {
        private const int OFFSET = 10;
        private const double GRID_HEGTH_FACTOR = 0.9;
        
        private const int EXCEL_FIRST_ROW = 2;

        private List<UserInfo> m_users = new List<UserInfo>();
        private List<AdminInfo> m_admins = new List<AdminInfo>();

        private UserInfo m_user;
        private MoreInfoForm m_moreInfoForm;
        private CreateAdminForm m_createAdminForm;
        private ChangePasswordForm m_changePasswordForm;

        private ExcelManager m_excelManager;

        public UserPanel(UserInfo user)
        {
            InitializeComponent();
            m_user = user;
            ShowUserInfo();
        }

        public void DestroyPanel()
        {
            Controls.Clear();
        }

        private void ShowUserInfo()
        {
            m_usersDataGridView.Rows.Clear();


            for (int i = 0; i < m_user.GetTestResultsCount(); i++)
            {
                TestResult testResult = m_user.GetTestResult(i);

                String cholericString = testResult.cholericPercent.ToString() + "%";
                String sanguineString = testResult.sanguinePercent.ToString() + "%";
                String phlegmaticString = testResult.phlegmaticPercent.ToString() + "%";
                String melancholiacString = testResult.melancholiacPercent.ToString() + "%";

                String[] row = new String[] 
                { 
                    testResult.EndTime,
                    cholericString,
                    sanguineString,
                    phlegmaticString,
                    melancholiacString
                };

                m_usersDataGridView.Rows.Add(row);
            }
        }

        private UserInfo GetCurrentUserInfo()
        {
            String name = m_usersDataGridView.CurrentRow.Cells[1].Value.ToString();
            String surname = m_usersDataGridView.CurrentRow.Cells[0].Value.ToString();
            String patronymic = m_usersDataGridView.CurrentRow.Cells[2].Value.ToString();

            for (int i = 0; i < m_users.Count; i++)
            {
                UserInfo user = m_users[i];
               
                if ((name == user.Name) && 
                    (surname == user.Surname) &&
                    (patronymic == user.Patronymic))
            	{
                    return user;
            	}
            }
            return null;
        }

        private void OnResized(object sender, EventArgs e)
        {
            m_usersDataGridView.Width = Width - m_usersDataGridView.Location.X * 2;
            m_usersDataGridView.Height = Convert.ToInt32(Convert.ToDouble(Height) * GRID_HEGTH_FACTOR);
        }

        private void OnSaveToExcel(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel Files (*.xlsx) | *.xlsx";
            saveFileDialog.DefaultExt = "xlsx";
            
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                ExportDataToExcel(saveFileDialog.FileName);
            }
        }

        private void ExportDataToExcel(string fileName)
        {
            m_excelManager = new ExcelManager();

            CreateExcelTableStructure();
            ExportUserInfoToExcel();

            m_excelManager.SaveAs(fileName);
            m_excelManager.CloseExcel();
        }
        
        private void CreateExcelTableStructure()
        {
            if (m_excelManager != null)
            {
                m_excelManager.SetCellValue("1", "A", "Фамилия");
                m_excelManager.SetCellValue("1", "B", "Имя");
                m_excelManager.SetCellValue("1", "C", "Отчество");
                m_excelManager.SetCellValue("1", "D", "Последние действия");
                m_excelManager.SetCellValue("1", "E", "Дата начала");
                m_excelManager.SetCellValue("1", "F", "Дата окончания");
                m_excelManager.SetCellValue("1", "G", "Холерик (%)");
                m_excelManager.SetCellValue("1", "H", "Сангвиник (%)");
                m_excelManager.SetCellValue("1", "I", "Флегматик (%)");
                m_excelManager.SetCellValue("1", "J", "Меланхолик (%)");

                m_excelManager.SetColumnsWidth(20);
                m_excelManager.SetRangeFontBoldProperty("A1", "J1", true);

                m_excelManager.SetRangeBottomLineWeight("A1", "J1", 4);
            }
        }

        private void ExportUserInfoToExcel()
        {
            UserInfo userInfo = m_user;
            int testsNum = Math.Max(userInfo.GetTestResultsCount(), 1);
            int startRow = EXCEL_FIRST_ROW;
            int finishRow = startRow + testsNum;

            m_excelManager.SetCellValue(startRow.ToString(), "A", userInfo.Surname);
            m_excelManager.MergeCells("A" + startRow.ToString(), "A" + (finishRow - 1).ToString());

            m_excelManager.SetCellValue(startRow.ToString(), "B", userInfo.Name);
            m_excelManager.MergeCells("B" + startRow.ToString(), "B" + (finishRow - 1).ToString());

            m_excelManager.SetCellValue(startRow.ToString(), "C", userInfo.Patronymic);
            m_excelManager.MergeCells("C" + startRow.ToString(), "C" + (finishRow - 1).ToString());

            m_excelManager.SetCellValue(startRow.ToString(), "D", userInfo.LastAccountUsing);
            m_excelManager.MergeCells("D" + startRow.ToString(), "D" + (finishRow - 1).ToString());

            for (int i = startRow; i < finishRow; i++)
            {
                ExportTestResultToExcel(userInfo.GetTestResult(i - startRow), i.ToString());
            }
            if (userInfo.GetTestResultsCount() == 0)
            {
                m_excelManager.SetCellValue(startRow.ToString(), "G", "Тест не пройден");
                m_excelManager.SetCellValue(startRow.ToString(), "H", "Тест не пройден");
                m_excelManager.SetCellValue(startRow.ToString(), "I", "Тест не пройден");
                m_excelManager.SetCellValue(startRow.ToString(), "J", "Тест не пройден");
            }


            m_excelManager.SetRangeBottomLineWeight("A" + startRow.ToString(), "J" + (finishRow - 1).ToString(), 3);
        }

        private void ExportTestResultToExcel(TestResult testResult, string row)
        {
            m_excelManager.SetCellValue(row, "E", testResult.BeginTime);
            m_excelManager.SetCellValue(row, "F", testResult.EndTime);
            m_excelManager.SetCellValue(row, "G", testResult.cholericPercent.ToString());
            m_excelManager.SetCellValue(row, "H", testResult.sanguinePercent.ToString());
            m_excelManager.SetCellValue(row, "I", testResult.phlegmaticPercent.ToString());
            m_excelManager.SetCellValue(row, "J", testResult.melancholiacPercent.ToString());
        }
    }
}